package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.RedisConstants;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.wemedia.dtos.HotArticleVo;
import com.heima.utils.common.BeanHelper;
import com.heima.utils.common.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired(required = false)
    private ApArticleMapper apArticleMapper;
    @Autowired(required = false)
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired(required = false)
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadApArticle(ArticleHomeDto dto, int type) {
        //处理参数
        if(StringUtils.isEmpty(dto.getTag()))dto.setTag("__all__");
        if(dto.getMinBehotTime()==null)dto.setMinBehotTime(new Date());
        if(dto.getMaxBehotTime()==null)dto.setMaxBehotTime(new Date());
        if(dto.getSize()==null)dto.setSize(10);
        //查询数据
        List<ApArticle> articleList = apArticleMapper.loadApArticle(dto, type);

        return ResponseResult.okResult(articleList);

    }

    @Override
    public Long saveOrUpdateApArticle(ArticleDto dto) {
        ApArticle apArticle = BeanHelper.copyProperties(dto, ApArticle.class);
        if (dto.getId() == null) {
            apArticleMapper.insert(apArticle);

            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);

            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsForward(true);
            apArticleConfig.setIsComment(true);
            apArticleConfigMapper.insert(apArticleConfig);

        } else {
            //修改ap_article表
            updateById(apArticle);

            //修改ap_content表
            QueryWrapper<ApArticleContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("article_id",apArticle.getId());
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(queryWrapper);
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }

        //重新文章静态页
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());

        return apArticle.getId();
    }

    @Override
    public ResponseResult loadApArticleFromCache(ArticleHomeDto dto, Integer type) {
        String key = RedisConstants.HOT_ARTICLE_FIRST_PAGE+dto.getTag();
        //从Redis数据库查询
        String redisData = redisTemplate.opsForValue().get(key);
        //如果Redis缓存有数据，则直接返回Redis数据
        if(StringUtils.isNotEmpty(redisData)){
            List<HotArticleVo> hotArticleVos = JsonUtils.toList(redisData,HotArticleVo.class);
            if(CollectionUtils.isNotEmpty(hotArticleVos)){
                List<ApArticle> apArticleList = BeanHelper.copyWithCollection(hotArticleVos,ApArticle.class);
                return ResponseResult.okResult(apArticleList);
            }
        }
        //如果Redis缓存没有数据，读取DB数据
        return loadApArticle(dto,type);
    }
}
